package com.android.clinic.doctors.app.ui.inquiry;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.clinic.doctors.app.ClinicDoctorsApplication;
import com.android.clinic.doctors.app.R;
import com.android.clinic.doctors.app.adapter.BaseListAdapter;
import com.android.clinic.doctors.app.base.BaseRefreshListActivity;
import com.android.clinic.doctors.app.entity.InquiryInfo;
import com.android.clinic.doctors.app.glide.ImageLoaderUtils;
import com.android.clinic.doctors.app.http.HttpClient;
import com.android.clinic.doctors.app.http.base.BaseObserver;
import com.android.clinic.doctors.app.http.utils.RxObservableUtils;
import com.android.clinic.doctors.app.ui.widget.UIAlertView;
import com.android.clinic.doctors.app.utils.Utils;
import com.kennyc.view.MultiStateView;

import java.util.List;

/**
 * 图文问诊列表
 * Created by cheng on 2018/6/13.
 */
public class GraphicInquiryListActivity extends BaseRefreshListActivity<InquiryInfo> {

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_refresh_listview;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setTitleBarTitle("图文问诊");
        mAdapter.setOnItemClickListener((view, position) -> {
           if (!"2".equals(mAdapter.getItem(position).getStatus())) {
               Intent intent = new Intent(GraphicInquiryListActivity.this, InquiryBillActivity.class);
               intent.putExtra("inquiryInfo", mAdapter.getItem(position));
               startActivityForResult(intent, InquiryBillActivity.REQ_CLASSIFY);
           } else {
               showToast("该次问诊已经结束了！");
           }
        });
    }

    @Override
    protected void reloadData() {
        HttpClient.getHttpService().getListAllInquiry(ClinicDoctorsApplication.getUserInfo().getId())
                .compose(RxObservableUtils.applySchedulers())
                .subscribe(new BaseObserver<List<InquiryInfo>>() {
                    @Override
                    public void onSuccess(List<InquiryInfo> inquiryInfo) {
                        refreshComplete();
                        if (null != inquiryInfo && inquiryInfo.size() > 0) {
                            setMultiStateView(MultiStateView.VIEW_STATE_CONTENT);
                            mAdapter.addAll(inquiryInfo);
                        } else if (pageNum == 1) {
                            setMultiStateView(MultiStateView.VIEW_STATE_EMPTY);
                        }
                    }

                    @Override
                    public void onFailure(String message) {
                        refreshComplete();
                        showToast(message);
                    }
                });
    }

    @Override
    protected BaseListAdapter<InquiryInfo> getAdapter() {
        return new InquiryAdapter(this);
    }

    private class InquiryAdapter extends BaseListAdapter<InquiryInfo> {

        public InquiryAdapter(Context context) {
            super(context, R.layout.item_list_graphic_inquiry);
        }

        @Override
        protected Object setupViewHolder(@Nullable View convertView, ViewGroup parent, int typeIndex) {
            return new ViewHolder(convertView);
        }

        @Override
        protected void setupBindViewHolder(InquiryInfo object, Object viewHolder, int position, int typeIndex) {
            ViewHolder holder = (ViewHolder) viewHolder;
            holder.tvName.setText(object.getNickName());
            holder.tvDescription.setText(object.getDescription());
            holder.tvTime.setText(Utils.getFormatTime(getItem(position).getCreateTime()));
            ImageLoaderUtils.getInstance().loadHeadImage(object.getImgUrl(), holder.ivHead);

            if ("2".equals(object.getStatus())) {
                holder.tvEndInquiry.setVisibility(View.VISIBLE);
                holder.btnEndInquiry.setVisibility(View.GONE);
            } else {
                holder.btnEndInquiry.setVisibility(View.VISIBLE);
                holder.tvEndInquiry.setVisibility(View.GONE);
                holder.btnEndInquiry.setOnClickListener(v -> UIAlertView.alertDialog(GraphicInquiryListActivity.this, "提示",
                        "确认结束本次问诊嘛?", new UIAlertView.OnThemeDialogListener() {
                            @Override
                            public void OnDismissButton() {

                            }
                            @Override
                            public void OnSureButton() {
                                closeInquiry(getItem(position).getId(), getItem(position).getNoticeId());
                            }
                        },"确定","取消"));
            }


        }

        class ViewHolder extends BaseViewHolder {

            TextView tvTime, tvName, tvDescription, tvEndInquiry;
            ImageView ivHead;
            Button btnEndInquiry;

            public ViewHolder(View itemView) {
                super(itemView);
                tvTime = itemView.findViewById(R.id.tv_time);
                tvName = itemView.findViewById(R.id.tv_name);
                tvDescription = itemView.findViewById(R.id.tv_description);
                ivHead = itemView.findViewById(R.id.iv_patient_head);
                btnEndInquiry = itemView.findViewById(R.id.btn_end_inquiry);
                tvEndInquiry = itemView.findViewById(R.id.tv_end_inquiry);
            }
        }

    }

    /**
     * 结束问诊
     * @param inquiryId
     */
    private void closeInquiry(String inquiryId, String noticeId) {
        HttpClient.getHttpService().closeInquire(inquiryId, noticeId)
                .compose(RxObservableUtils.applySchedulers())
                .subscribe(new BaseObserver<String>() {
                    @Override
                    public void onSuccess(String s) {
                        new Handler().postDelayed(() -> swipeToLoadLayout.post(() ->
                                swipeToLoadLayout.setRefreshing(true)), 200);
                    }

                    @Override
                    public void onFailure(String message) {
                        showToast(message);
                    }

                });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == InquiryBillActivity.REQ_CLASSIFY && resultCode == RESULT_OK) {
            new Handler().postDelayed(() -> swipeToLoadLayout.post(() ->
                    swipeToLoadLayout.setRefreshing(true)), 200);
        }

    }
}
